//
//  AddCarViewController.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/13.
//

#import "AddCarViewController.h"
#import "CyCStandardCell.h"
#import "CycSwitchCell.h"
#import "CycBLEManager.h"
#import "CycCarModel.h"
#import "TestViewController.h"


@interface AddCarViewController ()<UITableViewDataSource , UITableViewDelegate>

@property (nonatomic , strong)UITableView *tableView;
@property (nonatomic , strong)UIButton *skipBtn;
@property (nonatomic , strong) NSMutableArray *scanDevices; // 搜索到的设备

@end

@implementation AddCarViewController

- (NSMutableArray *)scanDevices {
    if(!_scanDevices){
        _scanDevices = [NSMutableArray arrayWithCapacity:1];
    }
    return _scanDevices;
}

-(UIButton *)skipBtn {
    if (!_skipBtn) {
        _skipBtn = [[UIButton alloc] init];
        [_skipBtn setTitle:@"跳过 >>" forState:(UIControlStateNormal)];
        _skipBtn.backgroundColor = COLOR_TOPIC_SELECT;
        _skipBtn.titleLabel.font = FONT_BODY_16;
        _skipBtn.titleLabel.textColor = COLOR_WHITE;
        _skipBtn.layer.cornerRadius = 30;
        _skipBtn.layer.masksToBounds = YES;
    }
    return _skipBtn;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - NavigationBarHeight)style:(UITableViewStyleGrouped)];
        [_tableView registerClass:[CyCStandardCell class] forCellReuseIdentifier:@"CyCStandardCell"];
        [_tableView registerClass:[CycSwitchCell class] forCellReuseIdentifier:@"CycSwitchCell"];
        _tableView.backgroundColor = COLOR_APP_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = HEIGHT_CELL48;
    }
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];

    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [[CycBLEManager sharedManager] stopScan];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加设备";
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.skipBtn];
    [self.skipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-30);
        make.left.equalTo(self.view).offset(30);
        make.bottom.equalTo(self.view).offset(-SafeAreaBottomHeight-30);
        make.centerX.mas_equalTo(self.view.ce_centerX);
        make.height.mas_equalTo(60);
    }];
    
    [[CycBLEManager sharedManager] startScanWithSuccess:^(NSArray<CBPeripheral *> *peripherals) {
        NSLog(@"-=-==============%@",peripherals);
        self.scanDevices = [peripherals copy];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"====================%@",error);
    }];
}

#pragma mark ==== datasource代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if(section == 3){
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 60)];
        UILabel *titleLB = [[UILabel alloc] initWithFrame:CGRectMake(MARGIN_MAX, 20, SCREEN_W, 30)];
        titleLB.font = FONT_PRIMARY_TITLE;
        titleLB.text = @"搜索结果";
        titleLB.textColor = COLOR_BLACK;
        [header addSubview:titleLB];
        
        return header;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if(section == 3){
        return 60;
    }
    return CGFLOAT_MIN;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0 || section == 1 || section == 2){
        return 1;
    }
    return self.scanDevices.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1) {
        CycSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CycSwitchCell" forIndexPath:indexPath];
        cell.backgroundColor = COLOR_APP_BACKGROUND;
        cell.titleLB.font = FONT_PRIMARY_TITLE;
        cell.titleLB.textColor = COLOR_BLACK;
        cell.titleLB.text = indexPath.section == 0 ? @"蓝牙" : @"已添加设备";
        
        return cell;
    }else {
        CyCStandardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CyCStandardCell" forIndexPath:indexPath];
        [cell setCurrentIndex:0 cellCount:1];
        cell.cellType = CycStandardCellTypeTitle;
        cell.titleLB.font = FONT_PRIMARY_TITLE;
        cell.titleLB.textColor = COLOR_NOT_SELECT;
        cell.subTitleLB.font = FONT_PRIMARY_TITLE;
        cell.subTitleLB.textColor = COLOR_TOPIC_SELECT;
        if(indexPath.section == 3){
            CBPeripheral *peripherals = self.scanDevices[indexPath.row];
            cell.titleLB.text = peripherals.name;
        }else {
            cell.titleLB.text = @"已连接设备";
        }
        cell.subTitleLB.text = @"连接";
        
        return cell;
    }
}

#pragma mark ==== delegate代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 3){
        CBPeripheral *peripheral = self.scanDevices[indexPath.row];
        [[CycBLEManager sharedManager] connectPeripheral:peripheral success:^(CBPeripheral *peripheral) {
            NSLog(@"======= 连接成功");
            CycCarModel *model = [[CycCarModel alloc] init];
            model.peripheral = peripheral;
            TestViewController *vc = [[TestViewController alloc] init];
            vc.car = model;
            [self.navigationController pushViewController:vc animated:YES];

        } failure:^(NSError *error) {
            NSLog(@"======= 连接失败");
        }];
    }

}


@end
